# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html

import redis
import scrapy
import requests as rq
from scrapy.exceptions import DropItem
from scrapy.pipelines.images import ImagesPipeline


class TotalPipeline(object):
    def __init__(self):
        self.r = redis.Redis(password='5tgbnhy67ujm', db='1')

    def process_item(self, item, spider):
        if spider.name == 'total_url':
            all = dict(item)
            self.r.lpush('onepiece:page_url', all['t_url'])
        elif spider.name == 'img_down':
            all = dict(item)
            self.r.lpush('onepiece:img_url', all['down_url'])
        else:
            pass


class ImgPipeline(ImagesPipeline):
    def get_media_requests(self, item, info):
        if 'down_url' in item.keys():
            yield scrapy.Request(item['down_url'], meta=item)

    def file_path(self, request, response=None, info=None):
        item = request.meta
        if 'p_name' in item.keys():
            img_name = './%s/' % item['f_path'] + item['p_name'].strip('') + '.jpg'
            return img_name
        else:
            img_name = None
            return None


class GetipPipeline(object):
    def __init__(self):
        self.r = redis.Redis(password='5tgbnhy67ujm', db='3')

    def process_item(self, item, spider):
        if spider.name == 'xici' or spider.name == 'kuai':
            ips = dict(item)
            if ips['protocol'] == 'http':
                check = {'http': '%s://%s:%s' % (ips['protocol'], ips['host'], ips['port'])}
            elif ips['protocol'] == 'https':
                check = {'https': '%s://%s:%s' % (ips['protocol'], ips['host'], ips['port'])}
            try:
                if rq.get('http://www.baidu.com', proxies=check, timeout=2).status_code == 200:
                    self.r.lpush('ip:%s' % ips['protocol'], check[ips['protocol']])
                return item
            except:
                raise DropItem('%s is error' % check)
        else:
            pass


class OnepiecePipeline(object):
    def process_item(self, item, spider):
        return item
